package service

import (
	"context"

	pb "Cosmos.Communication/api/v1"
	"Cosmos.Communication/internal/biz"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

type ProcessorHealthServiceService struct {
	pb.UnimplementedProcessorHealthServiceServer
	responbility Responsibility
}

func NewProcessorHealthServiceService(responbility Responsibility) *ProcessorHealthServiceService {
	return &ProcessorHealthServiceService{responbility: responbility}
}

func (s *ProcessorHealthServiceService) HealthCheck(ctx context.Context, req *pb.ProcessorHealthCheckRequest) (result *pb.Result, err error) {
	result = &pb.Result{
		Code: 200,
	}
	if req == nil {
		result.Code = 400
		return
	}

	callReq := &biz.HealthCheckRequest{}
	callResult, err := s.responbility.Check(ctx, callReq)
	if err != nil {
		result.Code = 500
		result.Message = &wrapperspb.StringValue{Value: err.Error()}
		return
	}
	if callResult == nil {
		result.Code = 500
		result.Message = &wrapperspb.StringValue{Value: "healthCheck result is nil"}
		return
	}
	if callResult.Code != 200 {
		result.Code = int32(callResult.Code)
		result.Message = &wrapperspb.StringValue{Value: callResult.Message}
		return
	}

	return
}
